package com.xzy.controller;

import com.xzy.pojo.TopicVote;
import com.xzy.pojo.VoteUser;
import com.xzy.service.TopicVoteService;
import com.xzy.service.VoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TopicVoteController {
    @Autowired
    TopicVoteService topicVoteService;
    @Autowired
    VoteUserService voteUserService;

    @RequestMapping("/user/voteList")
    public String voteList(int nowPage, Model model,HttpSession session) {
        if (nowPage <= 0) {
            nowPage = 1;
        }
        List<TopicVote> topicVotes = this.topicVoteService.selectTopicVotePage((nowPage - 1) * 5, 5);
        VoteUser voteUser= (VoteUser) session.getAttribute("loginUser");
        model.addAttribute("voteUserName",voteUser.getUsername());
        model.addAttribute("nowPage", nowPage);
        model.addAttribute("topicVotes", topicVotes);
        return "voteList";
    }
    //    投票
    @RequestMapping("/user/vote/1")
    public String voteYes(Model model, HttpSession session, Integer topicId ,Integer nowPage) {
        Map<String, Object> map = new HashMap<String, Object>();
        VoteUser voteUser = (VoteUser) session.getAttribute("loginUser");
        String userTopicId=","+topicId;
        ++nowPage;
        map.put("username", voteUser.getUsername());
        map.put("userTopicId", userTopicId);
        map.put("topicId", topicId);
        VoteUser voteUser1 = this.voteUserService.selectVoteUserByName(voteUser.getUsername());
        //        判断用户是否已经投票了
        if(voteUser1.getTopicId().contains(String.valueOf(topicId))) {
            model.addAttribute("voteMsg", "此题目"+voteUser.getUsername()+"已投票，不能在投票了！！");
        }else {
            int res=this.topicVoteService.TopicVoteYes(map);
            if (res < 0) {

            } else {
                model.addAttribute("voteMsg", voteUser.getUsername()+"投票成功");
            }
        }
        return "forward:/user/voteList?nowPage="+nowPage;
    }
    @RequestMapping("/user/vote/2")
    public String voteNo(Model model, HttpSession session, Integer topicId ,Integer nowPage) {
        Map<String, Object> map = new HashMap<String, Object>();
        VoteUser voteUser = (VoteUser) session.getAttribute("loginUser");
        String userTopicId=","+topicId;
        ++nowPage;
        map.put("username", voteUser.getUsername());
        map.put("userTopicId", userTopicId);
        map.put("topicId", topicId);
        VoteUser voteUser1 = this.voteUserService.selectVoteUserByName(voteUser.getUsername());
        //        判断用户是否已经投票了
        if(voteUser1.getTopicId().contains(String.valueOf(topicId))) {
            model.addAttribute("voteMsg", "此题目"+voteUser.getUsername()+"已投票，不能在投票了！！");
        }else {
            int res=this.topicVoteService.TopicVoteNo(map);
            if (res < 0) {

            } else {
                model.addAttribute("voteMsg", voteUser.getUsername()+"投票成功");
            }
        }
        return "forward:/user/voteList?nowPage="+nowPage;
    }
    @RequestMapping("/user/vote/3")
    public String voteNoKnow(Model model, HttpSession session, Integer topicId ,Integer nowPage) {
        Map<String, Object> map = new HashMap<String, Object>();
        VoteUser voteUser = (VoteUser) session.getAttribute("loginUser");
        String userTopicId=","+topicId;
        ++nowPage;
        map.put("username", voteUser.getUsername());
        map.put("userTopicId", userTopicId);
        map.put("topicId", topicId);
        VoteUser voteUser1 = this.voteUserService.selectVoteUserByName(voteUser.getUsername());
        //        判断用户是否已经投票了
        if(voteUser1.getTopicId().contains(String.valueOf(topicId))) {
            model.addAttribute("voteMsg", "此题目"+voteUser.getUsername()+"已投票，不能在投票了！！");
        }else {
            int res=this.topicVoteService.TopicVoteNoKnow(map);
            if (res < 0) {

            } else {
                model.addAttribute("voteMsg", voteUser.getUsername()+"投票成功");
            }
        }
        return "forward:/user/voteList?nowPage="+nowPage;
    }
    @RequestMapping("/user/addTopic/to")
    public String addTopic(){
        return "addTopic";
    }
    @RequestMapping("/user/addTopic/author")
    public String addTopic(String topicName,HttpSession session,Model model){
        VoteUser voteUser = (VoteUser) session.getAttribute("loginUser");
        String author=voteUser.getUsername();
        try{
            int res=this.topicVoteService.InsertTopic(topicName,author);
            if(res>0){
                model.addAttribute("addTopic","用户"+author+"已成功添加题目");
            }else {
                model.addAttribute("addTopic","用户"+author+"添加题目失败，题目已存在");
            }
        }catch (Exception e){
            model.addAttribute("addTopic","用户"+author+"添加题目失败，题目已存在");
        }
        return "addTopic";
    }
}
